#coding:utf-8
import os
import shutil
import time
import datetime
 
SOURCE = os.path.abspath(r'D:\Program Files (x86)\Steam\userdata\39454592\1446780')
TARGET = os.path.abspath(r'D:\作弊\怪猎\存档\\')


def check_file_max(target_path, number):
    dirList = os.listdir(target_path)
    dirList.sort()

    eraseNum = len(dirList) - number
    print(eraseNum)
    for i in range(0, eraseNum - 1):
        shutil.rmtree(target_path + '\\' + dirList[i])
        print('erase old file %s', target_path + dirList[i])

def do_cpy(source_path, target_path):
    if not os.path.exists(target_path):
        # 如果目标路径不存在原文件夹的话就创建
        os.makedirs(target_path)
    now = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
    target_path = target_path + '\\' +now
    if os.path.exists(target_path):
        shutil.rmtree(target_path)
    shutil.copytree(source_path, target_path)

    print('copy dir finished! %s %s', source_path, target_path)

if __name__ == "__main__":
    check_file_max(TARGET, 10)
    do_cpy(SOURCE, TARGET)
    
